﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using bts_webapp.Models;

namespace bts_webapp
{
    public partial class UxProblem : System.Web.UI.UserControl
    {
        public string ProblemId
        {
            get
            {
                if (ViewState["ProblemId"] != null)
                    return ViewState["ProblemId"].ToString();
                return string.Empty;
            }
            set
            {
                ViewState["ProblemId"] = value;
            }
        }

        public string TramId
        {
            get
            {
                return this.comboTram.SelectedItem.Value;
            }
            set { this.UpdateStation(value); }
        }

        public string LuongId
        {
            get
            {
                return this.comboLuong.SelectedItem.Value;
            }
            set { this.UpdateLuong(value); }
        }

        public string ThietbiId
        {
            get
            {
                return this.comboThietBi.SelectedItem.Value;
            }
            set { this.UpdateThietbi(value); }
        }

        public string User_Id
        {
            get
            {
                return this.comboUser.SelectedItem.Value;
            }
            set { this.UpdateUser(value); }
        }

        public string ProblemName
        {
            get
            {
                return this.txtPoblemName.Text;
            }
            set { this.txtPoblemName.Text = value; }
        }
        //public string ProblemDate
        //{
        //    get
        //    {
        //        return this.txtProblemDate.NamingContainer ;
        //    }
        //    set { this.txtProblemDate.InvalidTextBoxValue = value; }
        //}
        public string ProblemContent
        {
            get
            {
                return this.txtProblemContent.Text;
            }
            set { this.txtProblemContent.Text = value; }
        }
        public string Note
        {
            get
            {
                return this.txtNote.Text;
            }
            set { this.txtNote.Text = value; }
        }
        
        public bool Status
        {
            get
            {
                return this.chkStatus.Checked;
            }
            set { this.chkStatus.Checked = value; }
        }

        public bool InsertOrUpdate
        {
            set
            {
                this.btnUpdate.CommandName = value ? "PerformInsert" : "Update";
                this.btnUpdate.Text = value ? "Insert" : "Update";
            }

        }

        public void EnableFieldCode(bool enabled)
        {
            this.comboTram.Enabled = enabled;
        }

        public void BindData(VIBA_SUCO problem)
        {
            this.ProblemId = problem.SuCo_Id.ToString();
            this.TramId = problem.Tram_Id.ToString();
            this.LuongId = problem.Luong_Id.ToString();
            this.ThietbiId = problem.ThietBi_Id.ToString ();
            this.User_Id = problem.User_Id.ToString();
            this.Note = problem.SuCo_GhiChu;
            this.ProblemName = problem.SuCo_TenSuCo;
            this.ProblemContent = problem.SuCo_NoiDungSuCo;
            this.Status = problem.Status == 0 ? false : true;
        }

        public VIBA_SUCO GetData()
        {
            VIBA_SUCO problem = new VIBA_SUCO
            {
                SuCo_Id=Convert.ToInt32(this.ProblemId),
                Tram_Id = Convert.ToInt32(this.TramId),
                Luong_Id =Convert.ToInt32( this.LuongId),
                ThietBi_Id=Convert.ToInt32(this.ThietbiId),
                User_Id = Convert.ToInt32( this.User_Id),
                SuCo_TenSuCo = this.ProblemName,
                SuCo_NoiDungSuCo = this.ProblemContent,
                SuCo_GhiChu=this.Note,
                Status = GetStatus(this.Status)
            };

            return problem;
        }

        private int GetStatus(bool status)
        {
            return status ? 1 : 0;
        }

        public void SetErrorStatus(string error)
        {
            if (error.Trim() != string.Empty)
                this.litStatus.Text = "<font color='red'>" + error + "</font>";
            else
                this.litStatus.Text = string.Empty;
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }


        private void UpdateStation(string stationId)
        {
            var item = this.comboTram.Items.FirstOrDefault(x => x.Value.Trim() == stationId.Trim());
            if (item != null)
            {
                var index = this.comboTram.Items.IndexOf(item);
                this.comboTram.SelectedIndex = index;
            }
        }

        private void UpdateLuong(string luongId)
        {
            var item = this.comboLuong.Items.FirstOrDefault(x => x.Value.Trim() == luongId.Trim());
            if (item != null)
            {
                var index = this.comboLuong.Items.IndexOf(item);
                this.comboLuong.SelectedIndex = index;
            }
        }

        private void UpdateThietbi(string thietbiId)
        {
            var item = this.comboThietBi.Items.FirstOrDefault(x => x.Value.Trim() == thietbiId.Trim());
            if (item != null)
            {
                var index = this.comboThietBi.Items.IndexOf(item);
                this.comboThietBi.SelectedIndex = index;
            }
        }

        private void UpdateUser(string userId)
        {
            var item = this.comboUser.Items.FirstOrDefault(x => x.Value.Trim() == userId.Trim());
            if (item != null)
            {
                var index = this.comboUser.Items.IndexOf(item);
                this.comboUser.SelectedIndex = index;
            }
        }

        public void AddComboBoxTramItems()
        {
            using (var db = new BTSModelDataContext())
            {
                var list = db.VIBA_TRAMs;
                this.comboTram.DataSource = list;
                this.comboTram.DataTextField = "Tram_Ten";
                this.comboTram.DataValueField = "Tram_Id";
                this.comboTram.DataBind();
                this.UpdateStation(this.TramId);
            }
        }

        public void AddComboBoxThietbi()
        {
            using (var db = new BTSModelDataContext())
            {
                var list = db.VIBA_THIET_BIs;
                this.comboThietBi.DataSource = list;
                this.comboThietBi.DataTextField = "ThietBi_Ten";
                this.comboThietBi.DataValueField = "ThietBi_Id";
                this.comboThietBi.DataBind();
                this.UpdateThietbi(this.ThietbiId);
            }
        }

        public void AddComboBoxLuong()
        {
            using (var db = new BTSModelDataContext())
            {
                var list = db.VIBA_LUONGs;
                this.comboLuong.DataSource = list;
                this.comboLuong.DataTextField = "Luong_Ten";
                this.comboLuong.DataValueField = "Luong_Id";
                this.comboLuong.DataBind();
                this.UpdateLuong(this.LuongId);
            }
        }

        public void AddComboBoxUser()
        {
            using (var db = new BTSModelDataContext())
            {
                var list = db.VIBA_Users;
                this.comboUser.DataSource = list;
                this.comboUser.DataTextField = "User_Full_Name";
                this.comboUser.DataValueField = "User_Id";
                this.comboUser.DataBind();
                this.UpdateUser(this.User_Id);
            }
        }

    }
}